package com.jpcc.vw.onlineRadio.utils;

import android.content.Context;

import com.google.gson.Gson;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.jpcc.vw.onlineRadio.application.MusicApplication;
import com.jpcc.vw.onlineRadio.constants.Constants;
import com.jpcc.vw.onlineRadio.db.DatabaseHelperOrm;
import com.jpcc.vw.onlineRadio.db.FavouriteAlbumDbBean;
import com.jpcc.vw.onlineRadio.db.FavouriteAlbumDbBeanOnline;
import com.jpcc.vw.onlineRadio.http.HttpClient;
import com.jpcc.vw.onlineRadio.model.IRadio;
import com.jpcc.vw.onlineRadio.model.bean.AddAlbumCollectionEntity;
import com.jpcc.vw.onlineRadio.model.bean.AlbumFavouriteBean;
import com.jpcc.vw.onlineRadio.model.bean.BaseBean;
import com.jpcc.vw.onlineRadio.model.bean.CommonFavouritePaiedHistoryEntity;
import com.jpcc.vw.onlineRadio.model.impl.RadioImpl;
import com.zhy.http.okhttp.callback.StringCallback;
/*Start by menglingqiang*/
import android.util.Log;

import java.io.IOException;
import java.util.Date;
import com.jpcc.vw.tracelog.TracelogInterface;
/*End by menglingqiang*/

import java.sql.SQLException;
import java.util.ArrayList;
/*Start by menglingqiang*/
import java.util.Date;
/*End by menglingqiang*/
import java.util.List;
import java.util.logging.Logger;

import okhttp3.Call;
import okhttp3.Response;

public class SyncFavUtils {

    private Context context;

    public interface onSyncFav {

        void onSyncFavSuccess();

        void onsyncFavFail();
    }

    /**
     * 同步收藏列表
     */
    public static void syncFav(Context context, onSyncFav callback) {
        IRadio model = new RadioImpl();
        mergeFavData(context, callback);
    }

    private void syncFav(){

    }


    private static void mergeFavData(Context context, onSyncFav callback) {
        DatabaseHelperOrm helper = DatabaseHelperOrm.getHelper(context);
        try {
            List<Long> favs = new ArrayList<>();
            List<Long> toAdd = new ArrayList<>();
            List<AddAlbumCollectionEntity> toAddEntitys = new ArrayList<>();
            List<String> radios = new ArrayList<>();
            List<Long> toDelete = new ArrayList<>();
            List<AddAlbumCollectionEntity> toDeleteAlbumRadios=new ArrayList<>();
            List<FavouriteAlbumDbBean> datas = helper.getFavouriteDao().queryBuilder().where().eq("userId", MusicApplication.GetUserId()).query();
            List<FavouriteAlbumDbBeanOnline> datasOnline = helper.getOnlineFavouriteDao().queryBuilder().where().eq("userId", MusicApplication.GetUserId()).query();
            List<Long> datasOnlineIds = new ArrayList<>();
            List<Long> datasLocalIds = new ArrayList<>();
            for (FavouriteAlbumDbBeanOnline da : datasOnline) {
                datasOnlineIds.add(da.getId());
            }
            for (FavouriteAlbumDbBean da : datas) {
                datasLocalIds.add(da.getId());
            }

            for (FavouriteAlbumDbBean dbBean : datas) {
                if (!datasOnline.contains(dbBean.getId())) {
                    toAdd.add(dbBean.getId());
                    AddAlbumCollectionEntity entity = new AddAlbumCollectionEntity();
                    entity.setId(dbBean.getId());
                    entity.setName(dbBean.getName());
                    entity.setHostName(dbBean.getHostName());
                    entity.setCoverUrlSmall(dbBean.getCoverUrlLarge());
                    entity.setCoverUrlMiddle(dbBean.getCoverUrlLarge());
                    entity.setCoverUrlLarge(dbBean.getCoverUrlLarge());
                    entity.setContentAbstract("");
                    entity.setCpSource("喜马拉雅");
                    entity.setCategoryId(0L);
                    entity.setPaid(false);
                    entity.setAuthorized(true);
                    entity.setOnline(true);
                    entity.setContentAbstract("XiMaLaYa");
                    entity.setTagName("XiMaLaYa");
                    entity.setCollecTimestamp(dbBean.getTimeStamp());
                    if("album".equals(dbBean.getType())){
                        entity.setType(0);
                    }
                    else{
                        entity.setType(2);
                    }
                    toAddEntitys.add(entity);
                }
            }


            for (FavouriteAlbumDbBeanOnline onlinedata : datasOnline) {
                if (!datasLocalIds.contains(onlinedata.getId())) {
                    toDelete.add(onlinedata.getId());
                    radios.add(onlinedata.getId() + "");
                    AddAlbumCollectionEntity toDeleteEntity=new AddAlbumCollectionEntity();
                    toDeleteEntity.setId(onlinedata.getId());
                    if("album".equals(onlinedata.getType())){
                        toDeleteEntity.setType(0);
                    }
                    else{
                        toDeleteEntity.setType(2);
                    }
                    toDeleteAlbumRadios.add(toDeleteEntity);
                }
            }


//            syncDataToServer(toAdd, toDelete, callback);
            syncDataWithServer(toAdd, toDelete, toAddEntitys, radios, callback,toDeleteAlbumRadios);
        } catch (SQLException e) {
            if (callback != null) {
                callback.onsyncFavFail();
            }
            e.printStackTrace();
        }
    }


    private static void syncDataWithServer(List<Long> toAdd, List<Long> toDelete, List<AddAlbumCollectionEntity> toAddEntitys, List<String> radios, onSyncFav callback,List<AddAlbumCollectionEntity> toDeleteEntities) {
        IRadio model = new RadioImpl();

        /*Start by menglingqiang*/
        Date dt = new Date();
        try {
            TracelogInterface inter = MusicApplication.getLogInterface();
            if(inter != null){
                inter.sendLogData(dt.toString(),dt.toString(),SyncFavUtils.class.getName(),"DEBUG","400","GetAlbumRadioFavourite Request");
            }else {
                Log.e("JPCC","inter == null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*End by menglingqiang*/

        model.GetAlbumRadioFavourite().subscribe(new CommonSubscriber<AlbumFavouriteBean>() {
            @Override
            public void onSuccess(AlbumFavouriteBean response_fav_online) {
                List<AddAlbumCollectionEntity> toAddFinl = new ArrayList<>();
                List<AddAlbumCollectionEntity> toDeleteFinalEntities = new ArrayList<>();
                List<Long> toAddFinalIds = new ArrayList<>();
                List<Long> currentOnlineIds = new ArrayList<>();
                List<Long> toDeleteFinal = new ArrayList<>();

                /*Start by menglingqiang*/
                Date dt = new Date();
                try {
                    TracelogInterface inter = MusicApplication.getLogInterface();
                    if(inter != null){
                        inter.sendLogData(dt.toString(),dt.toString(),SyncFavUtils.class.getName(),"DEBUG","400","GetAlbumRadioFavourite onSuccess");
                    }else {
                        Log.e("JPCC","inter == null");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                /*End by menglingqiang*/

                for (CommonFavouritePaiedHistoryEntity entity : response_fav_online.getDataALL()) {
                    currentOnlineIds.add(entity.getId());
                }
                for (Long id : toDelete) {
                    if (currentOnlineIds.contains(id)) {
                        toDeleteFinal.add(id);
                    }
                }
                for (Long id : toAdd) {
                    if (!currentOnlineIds.contains(id)) {
                        toAddFinalIds.add(id);
                    }
                }
                for (AddAlbumCollectionEntity entity : toAddEntitys) {
                    if (toAddFinalIds.contains(entity.getId())) {
                        toAddFinl.add(entity);
                    }
                }

                for (AddAlbumCollectionEntity entity : toDeleteEntities){
                    if (toDeleteFinal.contains(entity.getId())) {
                        toDeleteFinalEntities.add(entity);
                    }
                }

                if (toAddFinl.size() == 0 && toDeleteFinal.size() == 0) {
                    DatabaseHelperOrm helper = DatabaseHelperOrm.getHelper(MusicApplication.GetInstance());
                    DatabaseHelperOrm helper_online = DatabaseHelperOrm.getHelper(MusicApplication.GetInstance());
                    DeleteBuilder deleteBuilder = null;
                    DeleteBuilder deleteBuilder1 = null;
                    try {
                        deleteBuilder = helper.getFavouriteDao().deleteBuilder();
                        deleteBuilder.where().eq("userId", MusicApplication.GetUserId());
                        deleteBuilder.delete();
                        deleteBuilder1 = helper_online.getOnlineFavouriteDao().deleteBuilder();
                        deleteBuilder1.where().eq("userId", MusicApplication.GetUserId());
                        deleteBuilder1.delete();

                        /*Start by menglingqiang*/
                        //Date dt = new Date();
                        try {
                            TracelogInterface inter = MusicApplication.getLogInterface();
                            if(inter != null){
                                inter.sendLogData(dt.toString(),dt.toString(),SyncFavUtils.class.getClass().getName(),"DEBUG",null,"GetAlbumRadioFavourite Request");
                            }else {
                                Log.e("JPCC","inter == null");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        /*End by menglingqiang*/

                        model.GetAlbumRadioFavourite().subscribe(new CommonSubscriber<AlbumFavouriteBean>() {
                            @Override
                            public void onSuccess(AlbumFavouriteBean response_final) {
//                                for (CommonFavouritePaiedHistoryEntity entity : response_final.getData()) {
                                for(int index=0;index<response_final.getDataALL().size();index++){
                                    CommonFavouritePaiedHistoryEntity entity=response_final.getDataALL().get(index);
                                    FavouriteAlbumDbBean result = new FavouriteAlbumDbBean();
                                    result.setUserId(MusicApplication.GetUserId());
                                    result.setId(entity.getId());
                                    result.setHostName(entity.getHostName());
                                    if("album".equals(entity.getType())){
                                        result.setType("album");
                                        result.setName(entity.getName());
                                        result.setCoverUrlLarge(entity.getCover().getLarge().getUrl());
                                        result.setVipFree(entity.isVipFree());
                                        result.setSample(entity.isSample());
                                    }
                                    else{
                                        result.setType("radio");
                                        result.setName(entity.getTitle());
                                        result.setCoverUrlLarge(entity.getLargeCoverUrl());
                                    }
                                    result.setTimeStamp(0);
                                    try {
                                        helper.getFavouriteDao().create(result);
                                    } catch (SQLException e) {
                                        e.printStackTrace();
                                    }
                                    FavouriteAlbumDbBeanOnline result1 = new FavouriteAlbumDbBeanOnline();
                                    result1.setUserId(MusicApplication.GetUserId());
                                    result1.setId(entity.getId());

                                    result1.setHostName(entity.getHostName());
//                                    result1.setCoverUrlLarge(entity.getCover().getLarge().getUrl());
//                                    result1.setType("album");


                                    if("album".equals(entity.getType())){
                                        result1.setType("album");
                                        result1.setName(entity.getName());
                                        result1.setCoverUrlLarge(entity.getCover().getLarge().getUrl());
                                        result1.setVipFree(entity.isVipFree());
                                        result1.setSample(entity.isSample());
                                    }
                                    else{
                                        result1.setType("radio");
                                        result1.setName(entity.getTitle());
                                        result1.setCoverUrlLarge(entity.getLargeCoverUrl());
                                    }


                                    result1.setTimeStamp(0);
                                    try {
                                        helper.getOnlineFavouriteDao().create(result1);
                                    } catch (SQLException e) {
                                        e.printStackTrace();
                                    }
                                }
                                callback.onSyncFavSuccess();
                                /*Start by menglingqiang*/
                                Date dt = new Date();
                                try {
                                    TracelogInterface inter = MusicApplication.getLogInterface();
                                    if(inter != null){
                                        inter.sendLogData(dt.toString(),dt.toString(),SyncFavUtils.class.getName(),"DEBUG","400","GetAlbumRadioFavourite onSuccess");
                                    }else {
                                        Log.e("JPCC","inter == null");
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                /*End by menglingqiang*/
                            }

                            @Override
                            public void onFailure(BaseBean response) {
                                /*Start by menglingqiang*/
                                Date dt = new Date();
                                try {
                                    TracelogInterface inter = MusicApplication.getLogInterface();
                                    if(inter != null){
                                        inter.sendLogData(dt.toString(),dt.toString(),SyncFavUtils.class.getName(),"ERROR",response.getStatusCode().toString(),"GetRecommendation onFailure");
                                    }else {
                                        Log.e("JPCC","inter == null");
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                /*End by menglingqiang*/
                                if(callback!=null){
                                    callback.onsyncFavFail();
                                }
                            }
                        });
                    } catch (SQLException e) {
                        e.printStackTrace();
                        if(callback!=null){
                            callback.onsyncFavFail();
                        }
                    }
                } else {
                    if (toAddFinl.size() > 0) {


//                        model.AddUserFavourite(new Gson().toJson(toAddFinl)).subscribe(new CommonSubscriber<BaseBean>() {
//                            @Override
//                            public void onSuccess(BaseBean response) {
//                                com.orhanobut.logger.Logger.i("AddUserFavourite-->success");
//                            }
//
//                            @Override
//                            public void onFailure(BaseBean response) {
//                                com.orhanobut.logger.Logger.i("AddUserFavourite-->fail");
//                            }
//                        });










                        HttpClient.AddUserFavourite(new Gson().toJson(toAddFinl), new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                callback.onsyncFavFail();
                            }

                            @Override
                            public String parseNetworkResponse(Response response, int id) throws IOException {
                                return super.parseNetworkResponse(response, id);
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                BaseBean resBean = new Gson().fromJson(response, BaseBean.class);
                                if (Constants.STATUS_SUCCESS_CODE != resBean.getCode()) {
                                    if (callback != null) {
                                        callback.onsyncFavFail();
                                    }
                                } else {
                                    if (toDeleteFinal.size() > 0) {
                                        HttpClient.CancelUserFavourite(new Gson().toJson(toDeleteFinalEntities), new StringCallback() {
                                            @Override
                                            public void onError(Call call, Exception e, int id) {
                                                callback.onsyncFavFail();
                                            }

                                            @Override
                                            public void onResponse(String response, int id) {
                                                BaseBean baseResponse = new Gson().fromJson(response, BaseBean.class);
                                                if (Constants.STATUS_SUCCESS_CODE != baseResponse.getCode()) {
                                                    callback.onsyncFavFail();
                                                } else {
                                                    DatabaseHelperOrm helper = DatabaseHelperOrm.getHelper(MusicApplication.GetInstance());
                                                    DatabaseHelperOrm helper_online = DatabaseHelperOrm.getHelper(MusicApplication.GetInstance());
                                                    DeleteBuilder deleteBuilder = null;
                                                    DeleteBuilder deleteBuilder1 = null;
                                                    try {
                                                        deleteBuilder = helper.getFavouriteDao().deleteBuilder();
                                                        deleteBuilder.where().eq("userId", MusicApplication.GetUserId());
                                                        deleteBuilder.delete();
                                                        deleteBuilder1 = helper_online.getOnlineFavouriteDao().deleteBuilder();
                                                        deleteBuilder1.where().eq("userId", MusicApplication.GetUserId());
                                                        deleteBuilder1.delete();

                                                        /*Start by menglingqiang*/
                                                        Date dt = new Date();
                                                        try {
                                                            TracelogInterface inter = MusicApplication.getLogInterface();
                                                            if(inter != null){
                                                                inter.sendLogData(dt.toString(),dt.toString(),SyncFavUtils.class.getName(),"DEBUG",null,"GetAlbumRadioFavourite Request");
                                                            }else {
                                                                Log.e("JPCC","inter == null");
                                                            }
                                                        } catch (Exception e) {
                                                            e.printStackTrace();
                                                        }
                                                        /*End by menglingqiang*/

                                                        model.GetAlbumRadioFavourite().subscribe(new CommonSubscriber<AlbumFavouriteBean>() {
                                                            @Override
                                                            public void onSuccess(AlbumFavouriteBean response_final) {
                                                                for(int index=0;index<response_final.getDataALL().size();index++){
                                                                    CommonFavouritePaiedHistoryEntity entity=response_final.getDataALL().get(index);
                                                                    FavouriteAlbumDbBean result = new FavouriteAlbumDbBean();
                                                                    result.setUserId(MusicApplication.GetUserId());
                                                                    result.setId(entity.getId());
//                                                                    result.setName(entity.getName());
                                                                    result.setHostName(entity.getHostName());
//                                                                    result.setCoverUrlLarge(entity.getCover().getLarge().getUrl());
                                                                    if("album".equals(entity.getType())){
                                                                        result.setType("album");
                                                                        result.setName(entity.getName());
                                                                        result.setCoverUrlLarge(entity.getCover().getLarge().getUrl());
                                                                        result.setVipFree(entity.isVipFree());
                                                                        result.setSample(entity.isSample());
                                                                    }
                                                                    else{
                                                                        result.setType("radio");
                                                                        result.setName(entity.getTitle());
                                                                        result.setCoverUrlLarge(entity.getLargeCoverUrl());
                                                                    }

//                                                                    result.setType("album");
                                                                    result.setTimeStamp(0);
                                                                    try {
                                                                        helper.getFavouriteDao().create(result);
                                                                    } catch (SQLException e) {
                                                                        e.printStackTrace();
                                                                    }
                                                                    FavouriteAlbumDbBeanOnline result1 = new FavouriteAlbumDbBeanOnline();
                                                                    result1.setUserId(MusicApplication.GetUserId());
                                                                    result1.setId(entity.getId());
//                                                                    result1.setName(entity.getName());
                                                                    result1.setHostName(entity.getHostName());
//                                                                    result1.setCoverUrlLarge(entity.getCover().getLarge().getUrl());
//                                                                    result1.setType("album");
                                                                    if("album".equals(entity.getType())){
                                                                        result1.setType("album");
                                                                        result1.setName(entity.getName());
                                                                        result1.setCoverUrlLarge(entity.getCover().getLarge().getUrl());
                                                                        result1.setVipFree(entity.isVipFree());
                                                                        result1.setSample(entity.isSample());
                                                                    }
                                                                    else{
                                                                        result1.setType("radio");
                                                                        result1.setName(entity.getTitle());
                                                                        result1.setCoverUrlLarge(entity.getLargeCoverUrl());
                                                                    }
                                                                    result1.setTimeStamp(0);
                                                                    try {
                                                                        helper.getOnlineFavouriteDao().create(result1);
                                                                    } catch (SQLException e) {
                                                                        e.printStackTrace();
                                                                    }
                                                                }
                                                                callback.onSyncFavSuccess();
                                                                /*Start by menglingqiang*/
                                                                Date dt = new Date();
                                                                try {
                                                                    TracelogInterface inter = MusicApplication.getLogInterface();
                                                                    if(inter != null){
                                                                        inter.sendLogData(dt.toString(),dt.toString(),SyncFavUtils.class.getName(),"DEBUG","400","GetAlbumRadioFavourite onSuccess");
                                                                    }else {
                                                                        Log.e("JPCC","inter == null");
                                                                    }
                                                                } catch (Exception e) {
                                                                    e.printStackTrace();
                                                                }
                                                                /*End by menglingqiang*/
                                                            }

                                                            @Override
                                                            public void onFailure(BaseBean response) {
                                                                /*Start by menglingqiang*/
                                                                Date dt = new Date();
                                                                try {
                                                                    TracelogInterface inter = MusicApplication.getLogInterface();
                                                                    if(inter != null){
                                                                        inter.sendLogData(dt.toString(),dt.toString(),SyncFavUtils.class.getName(),"ERROR",response.getStatusCode().toString(),"GetRecommendation onFailure");
                                                                    }else {
                                                                        Log.e("JPCC","inter == null");
                                                                    }
                                                                } catch (Exception e) {
                                                                    e.printStackTrace();
                                                                }
                                                                /*End by menglingqiang*/
                                                                if(callback!=null){
                                                                    callback.onsyncFavFail();
                                                                }
                                                            }
                                                        });
                                                    } catch (SQLException e) {
                                                        e.printStackTrace();
                                                    }
                                                }
                                            }
                                        });
                                    } else {
                                        DatabaseHelperOrm helper = DatabaseHelperOrm.getHelper(MusicApplication.GetInstance());
                                        DatabaseHelperOrm helper_online = DatabaseHelperOrm.getHelper(MusicApplication.GetInstance());
                                        DeleteBuilder deleteBuilder = null;
                                        DeleteBuilder deleteBuilder1 = null;
                                        try {
                                            deleteBuilder = helper.getFavouriteDao().deleteBuilder();
                                            deleteBuilder.where().eq("userId", MusicApplication.GetUserId());
                                            deleteBuilder.delete();
                                            deleteBuilder1 = helper_online.getOnlineFavouriteDao().deleteBuilder();
                                            deleteBuilder1.where().eq("userId", MusicApplication.GetUserId());
                                            deleteBuilder1.delete();

                                            /*Start by menglingqiang*/
                                            Date dt = new Date();
                                            try {
                                                TracelogInterface inter = MusicApplication.getLogInterface();
                                                if(inter != null){
                                                    inter.sendLogData(dt.toString(),dt.toString(),SyncFavUtils.class.getName(),"DEBUG",null,"GetAlbumRadioFavourite Request");
                                                }else {
                                                    Log.e("JPCC","inter == null");
                                                }
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            }
                                            /*End by menglingqiang*/

                                            model.GetAlbumRadioFavourite().subscribe(new CommonSubscriber<AlbumFavouriteBean>() {
                                                @Override
                                                public void onSuccess(AlbumFavouriteBean response_final) {
                                                    for(int index=0;index<response_final.getDataALL().size();index++){
                                                        CommonFavouritePaiedHistoryEntity entity=response_final.getDataALL().get(index);
                                                        FavouriteAlbumDbBean result = new FavouriteAlbumDbBean();
                                                        result.setUserId(MusicApplication.GetUserId());
                                                        result.setId(entity.getId());
//                                                        result.setName(entity.getName());
                                                        result.setHostName(entity.getHostName());
//                                                        result.setCoverUrlLarge(entity.getCover().getLarge().getUrl());
//                                                        result.setType("album");
                                                        if("album".equals(entity.getType())){
                                                            result.setType("album");
                                                            result.setName(entity.getName());
                                                            result.setCoverUrlLarge(entity.getCover().getLarge().getUrl());
                                                            result.setVipFree(entity.isVipFree());
                                                            result.setSample(entity.isSample());
                                                        }
                                                        else{
                                                            result.setType("radio");
                                                            result.setName(entity.getTitle());
                                                            result.setCoverUrlLarge(entity.getLargeCoverUrl());
                                                        }
                                                        result.setTimeStamp(0);
                                                        try {
                                                            helper.getFavouriteDao().create(result);
                                                        } catch (SQLException e) {
                                                            e.printStackTrace();
                                                        }
                                                        FavouriteAlbumDbBeanOnline result1 = new FavouriteAlbumDbBeanOnline();
                                                        result1.setUserId(MusicApplication.GetUserId());
                                                        result1.setId(entity.getId());
//                                                        result1.setName(entity.getName());
                                                        result1.setHostName(entity.getHostName());
//                                                        result1.setCoverUrlLarge(entity.getCover().getLarge().getUrl());
//                                                        result1.setType("album");
                                                        if("album".equals(entity.getType())){
                                                            result1.setType("album");
                                                            result1.setName(entity.getName());
                                                            result1.setCoverUrlLarge(entity.getCover().getLarge().getUrl());
                                                            result1.setVipFree(entity.isVipFree());
                                                            result1.setSample(entity.isSample());
                                                        }
                                                        else{
                                                            result1.setType("radio");
                                                            result1.setName(entity.getTitle());
                                                            result1.setCoverUrlLarge(entity.getLargeCoverUrl());
                                                        }
                                                        result1.setTimeStamp(0);
                                                        try {
                                                            helper.getOnlineFavouriteDao().create(result1);
                                                        } catch (SQLException e) {
                                                            e.printStackTrace();
                                                        }


                                                    }

                                                    callback.onSyncFavSuccess();
                                                    /*Start by menglingqiang*/
                                                    Date dt = new Date();
                                                    try {
                                                        TracelogInterface inter = MusicApplication.getLogInterface();
                                                        if(inter != null){
                                                            inter.sendLogData(dt.toString(),dt.toString(),SyncFavUtils.class.getName(),"DEBUG","400","GetAlbumRadioFavourite onSuccess");
                                                        }else {
                                                            Log.e("JPCC","inter == null");
                                                        }
                                                    } catch (Exception e) {
                                                        e.printStackTrace();
                                                    }
                                                    /*End by menglingqiang*/
                                                }

                                                @Override
                                                public void onFailure(BaseBean response) {
                                                    /*Start by menglingqiang*/
                                                    Date dt = new Date();
                                                    try {
                                                        TracelogInterface inter = MusicApplication.getLogInterface();
                                                        if(inter != null){
                                                            inter.sendLogData(dt.toString(),dt.toString(),SyncFavUtils.class.getName(),"ERROR",response.getStatusCode().toString(),"GetAlbumRadioFavourite onFailure");
                                                        }else {
                                                            Log.e("JPCC","inter == null");
                                                        }
                                                    } catch (Exception e) {
                                                        e.printStackTrace();
                                                    }
                                                    /*End by menglingqiang*/


                                                    if(callback!=null){
                                                        callback.onsyncFavFail();
                                                    }
                                                }
                                            });
                                        } catch (SQLException e) {
                                            e.printStackTrace();
                                            if(callback!=null){
                                                callback.onsyncFavFail();
                                            }
                                        }
                                    }
                                }
                            }
                        });
                    } else {
                        HttpClient.CancelUserFavourite(new Gson().toJson(toDeleteFinalEntities), new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                callback.onsyncFavFail();
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                BaseBean baseResponse = new Gson().fromJson(response, BaseBean.class);
                                if (Constants.STATUS_SUCCESS_CODE != baseResponse.getCode()) {
                                    callback.onsyncFavFail();
                                } else {
                                    DatabaseHelperOrm helper = DatabaseHelperOrm.getHelper(MusicApplication.GetInstance());
                                    DatabaseHelperOrm helper_online = DatabaseHelperOrm.getHelper(MusicApplication.GetInstance());
                                    DeleteBuilder deleteBuilder = null;
                                    DeleteBuilder deleteBuilder1 = null;
                                    try {
                                        deleteBuilder = helper.getFavouriteDao().deleteBuilder();
                                        deleteBuilder.where().eq("userId", MusicApplication.GetUserId());
                                        deleteBuilder.delete();
                                        deleteBuilder1 = helper_online.getOnlineFavouriteDao().deleteBuilder();
                                        deleteBuilder1.where().eq("userId", MusicApplication.GetUserId());
                                        deleteBuilder1.delete();

                                        /*Start by menglingqiang*/
                                        Date dt = new Date();
                                        try {
                                            TracelogInterface inter = MusicApplication.getLogInterface();
                                            if(inter != null){
                                                inter.sendLogData(dt.toString(),dt.toString(),SyncFavUtils.class.getName(),"DEBUG",null,"GetAlbumRadioFavourite Request");
                                            }else {
                                                Log.e("JPCC","inter == null");
                                            }
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                        /*End by menglingqiang*/

                                        model.GetAlbumRadioFavourite().subscribe(new CommonSubscriber<AlbumFavouriteBean>() {
                                            @Override
                                            public void onSuccess(AlbumFavouriteBean response_final) {
//                                                for (CommonFavouritePaiedHistoryEntity entity : response_final.getData()) {
                                                for(int index=0;index<response_final.getDataALL().size();index++){
                                                    CommonFavouritePaiedHistoryEntity entity=response_final.getDataALL().get(index);
                                                    FavouriteAlbumDbBean result = new FavouriteAlbumDbBean();
                                                    result.setUserId(MusicApplication.GetUserId());
                                                    result.setId(entity.getId());
//                                                    result.setName(entity.getName());
                                                    result.setHostName(entity.getHostName());
//                                                    result.setCoverUrlLarge(entity.getCover().getLarge().getUrl());
//                                                    result.setType("album");
                                                    if("album".equals(entity.getType())){
                                                        result.setType("album");
                                                        result.setName(entity.getName());
                                                        result.setCoverUrlLarge(entity.getCover().getLarge().getUrl());
                                                        result.setVipFree(entity.isVipFree());
                                                        result.setSample(entity.isSample());
                                                    }
                                                    else{
                                                        result.setType("radio");
                                                        result.setName(entity.getTitle());
                                                        result.setCoverUrlLarge(entity.getLargeCoverUrl());
                                                    }
                                                    result.setTimeStamp(0);
                                                    try {
                                                        helper.getFavouriteDao().create(result);
                                                    } catch (SQLException e) {
                                                        e.printStackTrace();
                                                    }
                                                    FavouriteAlbumDbBeanOnline result1 = new FavouriteAlbumDbBeanOnline();
                                                    result1.setUserId(MusicApplication.GetUserId());
                                                    result1.setId(entity.getId());
//                                                    result1.setName(entity.getName());
                                                    result1.setHostName(entity.getHostName());
//                                                    result1.setCoverUrlLarge(entity.getCover().getLarge().getUrl());
//                                                    result1.setType("album");
                                                    if("album".equals(entity.getType())){
                                                        result1.setType("album");
                                                        result1.setName(entity.getName());
                                                        result1.setCoverUrlLarge(entity.getCover().getLarge().getUrl());
                                                        result1.setVipFree(entity.isVipFree());
                                                        result1.setSample(entity.isSample());
                                                    }
                                                    else{
                                                        result1.setType("radio");
                                                        result1.setName(entity.getTitle());
                                                        result1.setCoverUrlLarge(entity.getLargeCoverUrl());
                                                    }
                                                    result1.setTimeStamp(0);
                                                    try {
                                                        helper.getOnlineFavouriteDao().create(result1);
                                                    } catch (SQLException e) {
                                                        e.printStackTrace();
                                                    }
                                                }
                                                callback.onSyncFavSuccess();
                                                /*Start by menglingqiang*/
                                                Date dt = new Date();
                                                try {
                                                    TracelogInterface inter = MusicApplication.getLogInterface();
                                                    if(inter != null){
                                                        inter.sendLogData(dt.toString(),dt.toString(),SyncFavUtils.class.getName(),"DEBUG","400","GetAlbumRadioFavourite onSuccess");
                                                    }else {
                                                        Log.e("JPCC","inter == null");
                                                    }
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }
                                                /*End by menglingqiang*/
                                            }

                                            @Override
                                            public void onFailure(BaseBean response) {
                                                /*Start by menglingqiang*/
                                                Date dt = new Date();
                                                try {
                                                    TracelogInterface inter = MusicApplication.getLogInterface();
                                                    if(inter != null){
                                                        inter.sendLogData(dt.toString(),dt.toString(),SyncFavUtils.class.getName(),"ERROR",response.getStatusCode().toString(),"GetRecommendation onFailure");
                                                    }else {
                                                        Log.e("JPCC","inter == null");
                                                    }
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }
                                                /*End by menglingqiang*/
                                                if(callback!=null){
                                                    callback.onsyncFavFail();
                                                }
                                            }
                                        });
                                    } catch (SQLException e) {
                                        e.printStackTrace();
                                        if(callback!=null){
                                            callback.onsyncFavFail();
                                        }
                                    }
                                }
                            }
                        });
                    }
                }


            }

            @Override
            public void onFailure(BaseBean response) {
                callback.onsyncFavFail();
                /*Start by menglingqiang*/
                Date dt = new Date();
                try {
                    TracelogInterface inter = MusicApplication.getLogInterface();
                    if(inter != null){
                        inter.sendLogData(dt.toString(),dt.toString(),SyncFavUtils.class.getName(),"ERROR","400","GetAlbumRadioFavourite onFailure");
                    }else {
                        Log.e("JPCC","inter == null");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                /*End by menglingqiang*/
            }
        });
    }
}
